var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var template = require('../tpl/template')

var _nav = new nav();
var header = new _header();
$('.js_index').on('click',function(){
  location.href="./index.html";
})
var map ={
  isfirst:true,
  getsearch: function (medicalid) {
    var jingdu = util.getParam('jingdu');
    var weidu = util.getParam('weidu');
    var address = util.getParam('address') || '';
    var name = util.getParam('name') || '';
    if(jingdu && weidu && this.isfirst){
      this.isfirst = false;
      return {
        data: [{
          jingdu: jingdu,
          weidu: weidu,
          address: address,
          name: name
        }]
      }
    }
    return false;
  },
  init: function (medicalid) {
    this.init_BAIDU_MAP();
    this.getType();
    this.bindevent();
    // this.input_add();
    this.getlist();
  },
  maxpage:null,
  getType: function (medicalid) {
    var _this =this;
    util.fetch('/api/make/queryHospitalType',{
    }).then(function(res){
     var type = res.data.type || '';
     if(type && type.length){
       var str = '';
       for(var i=0;i<type.length;i++){
        str += _this.type_str.replace('{{id}}', type[i].id).replace(/\$\{name\}/g,type[i].name)
       }
       $('.js_map_doctor').append(str);
     }
     
    }).fail(function (res){
    })
  },
  hospital_type: '',
  page:1,
 
  getlist: function (medicalid) {
    var _this = this;
    util.fetch('api/make/queryHospital', {
      currentPage:_this.page,
      pageSize:10,
      hospital_type: _this.hospital_type || '',
      hospital_level:'',
      dept_name:'',
      hospital_name: _this.hospital_name || ''
    }).then(function(res){
      var data = res.data
      _this.maxpage = data.totalPageSize;
      if(data.data.length > 0){
        _this.render(data);
        _this.makerInfo(data);
      }else{
        alert('暂无数据，重新搜索！')
      }
      $('.js_doc').on('click',function(){
        sessionStorage.setItem('map',JSON.stringify(data.data[$(this).index()]))
       location.href='./mapxq.html?'
      })
    }).fail(function(res){
    })
  },
  render: function (data) {
    var tpl = template('map_list',{data:data.data})
    $('.js_doctor_list').html(tpl);
    if(this.maxpage <= 1){
      $('.js_btns').addClass('hide')
    }else {
      $('.js_num_wrap').html(this.page + '/'+ this.maxpage)
      $('.js_btns').removeClass('hide');
      if(this.page == 1){
        $('.js_btns .js_changepage_reduce').removeClass('blue')
        $('.js_btns .js_changepage_add').addClass('blue')
      }else if(this.page < this.maxpage){
        $('.js_btns .js_changepage_reduce').addClass('blue')
        $('.js_btns .js_changepage_add').addClass('blue')
      }else if(this.page == this.maxpage){
        $('.js_btns .js_changepage_reduce').addClass('blue')
        $('.js_btns .js_changepage_add').removeClass('blue')
      }else {
        $('.js_btns .js_changepage_add').addClass('blue')
      }
    }
  },
  type_str:'<li data-name="${name}" data-id="{{id}}">${name}</li>',
  bindevent: function (medicalid) {
    var _this = this;
    $('.js_map_doctor').on('click','li', function () {
      var name = $(this).html();
      $('.js_cont_wrap').html(name);
      $('.js_map_doctor').addClass('hide');
      _this.hospital_type = $(this).data().name || '';
      _this.page=1;
       _this.getlist();
    })
    $('.js_search_h').on('click',function(){
      _this.page=1;
      _this.hospital_name=$('.input_add').val();
      if(_this.hospital_name!=''){
        _this.getlist();
      }
    })
    $('.js_click_sj').on('click', function(){
      $('.js_map_doctor').toggleClass('hide');
    })
    $('.js_changepage_add').on('click',function(){
      if(_this.page < _this.maxpage){
        _this.page ++
        _this.getlist();
      }
    })
    $('.js_changepage_reduce').on('click',function(){
      if(_this.page > 1){
        _this.page --;
        _this.getlist();
      }
    })
  },
  marker:[],
  data_info:[],
  makerInfo: function (res) {
    var _this = this;
    var data = this.getsearch() || res.data;
   
    this.map.clearOverlays();
    this.marker = [];
    // if(this.marker.length > 0){
    //   for(var i = 0;i<this.marker.length;i++){
    //     this.map.removeOverlay(this.marker[i])
    //   }
    // }
    for(var i=0;i<data.length;i++){
      var item = data[i]
      
      this.marker[i] = null;
      this.marker[i] = new BMap.Marker(new BMap.Point(item.jingdu, item.weidu)); // 创建标注
      // this.marker[i].setAnimation(BMAP_ANIMATION_BOUNCE);
      this.map.addOverlay(this.marker[i])
      
      this.marker[i].mapTipsText = item.name;
      this.marker[i].mapAddress = item.address
      this.marker[i].addEventListener('click',function (e,i) {
       _this.openInfo(e,this)
      })
    }
    // this.map.setViewport(this.marker)
    if(data.length == 1){
      this.map.centerAndZoom(new BMap.Point(data[0].jingdu, data[0].weidu),16)
    }else {
      this.map.setZoom(12)
    }

    

  },
  openInfo: function (e,content){
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content.mapAddress, {
      width:250,
      height:80,
      title: content.mapTipsText,
      enableMessage:true
    }); // 创建信息窗口对象 
    this.map.openInfoWindow(infoWindow, point); //开启信息窗口
  },
  // input_add: function (medicalid) {
  //   var _this = this;
  //   $('.input_add').on('input propertychange',function () {
  //    _this.hospital_name = $(this).val();
  //    _this.getlist();
  //   })
  // },
  init_BAIDU_MAP: function (medicalid) {
    this.map = new BMap.Map("map_content");
    this.map.centerAndZoom(new BMap.Point(116.404, 39.915), 14); // 初始化地图,设置中心点坐标和地图级别
    var top_right_navigation = new BMap.NavigationControl({
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      type: BMAP_NAVIGATION_CONTROL_SMALL
    });
  
    this.map.addControl(top_right_navigation)
    this.map.setCurrentCity("北京"); // 设置地图显示的城市 此项是必须设置的
    // this.map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  }
}
map.init();